// 泛型: 当封装函数, 或者定义接口的时候, 如果某个数据类型不确定, 可以使用泛型提升灵活性.

// <>表示一个接口的参数.
interface IUser<T> {
  name: string;
  age: T;
}

let u1: IUser<number> = {
  name: "小明",
  // age: "20", // ts检查报错
  age: 22,
};

let u2: IUser<string> = {
  name: "小明",
  age: "20",
  // age: 22, // ts检查报错
};

let u3: IUser<string | number | boolean> = {
  name: "小明",
  age: 100,
};

// 需求: 一个函数, 参数可以是字符串, 也可以是数值, 如果是数值就求和; 如果是字符串就拼接.
function getResult<T>(a: T, b: T): T {
  // return a as any + b as any;
  return <any>a + <any>b;
}
console.log(getResult<number>(100, 200));
console.log(getResult<string>("小", "伙子"));

interface IMan<T, U> {
  name: T;
  age: U;
}
let man1: IMan<string, number> = {
  name: "123",
  age: 33,
};
